package com.douwong.student.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.douwong.student.R;
import com.douwong.student.model.ExamResult;
import com.douwong.student.model.ListPlExamineItemBean;
import com.douwong.student.model.OptionModel;
import com.douwong.student.model.Qditem;
import com.douwong.student.popuwindow.ShowClassTestRecordPopuWindow;
import com.douwong.student.utils.HtmlUtils;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @autor yichao
 * @time 2017/5/1016:55
 * @des 课堂测试记录 选择题
 */
@SuppressLint("ValidFragment")
public class BaseTestRecordChoiceFragment extends BaseFragment {
    private View mContenView;
    private AppCompatTextView mAppCompatTextView;
    private ImageView mIvHead;
    private LinearLayout mLlTitleRoot;
    private ListPlExamineItemBean mListPlExamineItemBean;
    private final Qditem mQdItem;
    protected int[] linearlayouts = new int[] { R.id.ll_A_choice, R.id.ll_B_choice, R.id.ll_C_choice, R.id.ll_D_choice,
            R.id.ll_E_choice, R.id.ll_F_choice, R.id.ll_G_choice, R.id.ll_H_choice };
    protected int[] textViews = new int[] { R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
            R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H};
    private List<ExamResult> mExamResultList;
    private String[] chart = { "A", "B", "C", "D", "E", "F", "G", "H" };
    private static final String TAG = "yichaos";
    private Context mContext;
    private ShowClassTestRecordPopuWindow mShowClassTestRecordPopuWindown;


    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void setExamResultList(List<ExamResult> examResultList) {
        super.setExamResultList(examResultList);
        mExamResultList = examResultList;
    }

    public BaseTestRecordChoiceFragment(ListPlExamineItemBean listPlExamineItemBean) {
        super();
        mListPlExamineItemBean=  listPlExamineItemBean;
        mQdItem = mListPlExamineItemBean.getQdItem();
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContenView = UIUtils.inflate(getActivity(), R.layout.base_test_record_choice_questions);
        mIvHead = (ImageView) mContenView.findViewById(R.id.iv_answer_show_pic);
        mAppCompatTextView = (AppCompatTextView) mContenView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContenView.findViewById(R.id.ll_choicefragme_title_root);
        return mContenView;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ShowClassTestRecordPopuWindow(mContext,mQdItem);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ShowClassTestRecordPopuWindow(mContext,mQdItem);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        if (mQdItem!=null){
            String itemBody = mQdItem.getItemBody();
            //是否显示图片
            mIvHead.setVisibility(HtmlUtils.isShowImage(itemBody)?View.VISIBLE:View.GONE);
            //题目
            HtmlUtils.showItemBody(mAppCompatTextView,itemBody);
            //选项
            ArrayList<OptionModel> optionModels = new ArrayList<OptionModel>();
            if (mQdItem.getItemOptions() != null && !mQdItem.getItemOptions().equals("")) {
                optionModels = (ArrayList<OptionModel>) JSON.parseArray(mQdItem.getItemOptions(), OptionModel.class);
            }
            for (int i = 0; i < optionModels.size(); i++) {
                OptionModel optionModel = optionModels.get(i);
                mContenView.findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
                //设置值
                TextView text = (TextView) mContenView.findViewById(textViews[i]);
                ImagFilterUtil.filterImage(text,optionModel.getValue(),1);
                HtmlUtils.showItemBody(text,optionModel.getValue());
            }
            //设置选中的item
            showResult();

        }
    }
    public void showResult() {
        //查看考试时显示

            for (int j = 0; j < linearlayouts.length; j++) {
                mContenView.findViewById(linearlayouts[j]).setClickable(false);

            if (mExamResultList == null) {
                return;
            }
                Log.i(TAG,"mExamResultList  :"+mExamResultList.toString() +"题目id"+mListPlExamineItemBean.getItemId());

                Log.i(TAG,"mListPlExamineItemBean"+mListPlExamineItemBean.getQdItem().getAnswers());
            for (int i = 0; i < mExamResultList.size(); i++) {
                if (mListPlExamineItemBean.getItemId() == mExamResultList.get(i).getItemId()) {


                    if (mExamResultList.get(i).getIsWaiver() == 1) {// 弃权

                    } else {

                        if (mQdItem.getItemType() == 1) {// 单选

                            setSingleViewState(mExamResultList.get(i).getAnswers());
                        } else if (mQdItem.getItemType() == 2) {// 多选
                            String str = mExamResultList.get(i).getAnswers();
                            String newStr = str.replace("*#*", "");
                            Log.e("str", str + "");
                            setMulteViewState(newStr);
                        }
                        break;
                    }

                }
            }
        }
    }

    // 单选设置控件状态
    private void setSingleViewState(String charts) {
        for (int i = 0; i < linearlayouts.length; i++) {
            if (charts.equalsIgnoreCase(chart[i])) {
                mContenView.findViewById(linearlayouts[i]).setSelected(true);
            } else {
                mContenView.findViewById(linearlayouts[i]).setSelected(false);
            }
        }
    }

    // 设置多选答案显示状态
    private void setMulteViewState(String charts) {
        for (int i = 0; i < linearlayouts.length; i++) {
            if (charts.contains(chart[i])) {
                mContenView.findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
                mContenView.findViewById(linearlayouts[i]).setSelected(true);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mShowClassTestRecordPopuWindown!=null && mShowClassTestRecordPopuWindown.isShowing()){
            mShowClassTestRecordPopuWindown.dismiss();

        }
    }
}
